package org.dromara.lsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.lsc.domain.DatasetLsc;
import org.dromara.lsc.domain.bo.DatasetLscBo;
import org.dromara.lsc.domain.vo.DatasetLscVo;
import org.dromara.lsc.mapper.DatasetLscMapper;
import org.dromara.lsc.service.IDatasetLscService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2023-12-08
 */
@RequiredArgsConstructor
@Service
public class DatasetLscServiceImpl implements IDatasetLscService {

    private final DatasetLscMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public DatasetLscVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<DatasetLscVo> queryPageList(DatasetLscBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DatasetLsc> lqw = buildQueryWrapper(bo);
        Page<DatasetLscVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<DatasetLscVo> queryList(DatasetLscBo bo) {
        LambdaQueryWrapper<DatasetLsc> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DatasetLsc> buildQueryWrapper(DatasetLscBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DatasetLsc> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDataName()), DatasetLsc::getDataName, bo.getDataName());
        lqw.eq(bo.getDataType() != null, DatasetLsc::getDataType, bo.getDataType());
        lqw.eq(bo.getStatus() != null, DatasetLsc::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDataFile()), DatasetLsc::getDataFile, bo.getDataFile());
        lqw.eq(bo.getDataSize() != null, DatasetLsc::getDataSize, bo.getDataSize());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(DatasetLscBo bo) {
        DatasetLsc add = MapstructUtils.convert(bo, DatasetLsc.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(DatasetLscBo bo) {
        DatasetLsc update = MapstructUtils.convert(bo, DatasetLsc.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DatasetLsc entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int updateStatus(DatasetLsc bo) {
        return baseMapper.update(
            new LambdaUpdateWrapper<DatasetLsc>()
                .eq(DatasetLsc::getId, bo.getId())
                .set(DatasetLsc::getStatus, bo.getStatus())
        );
    }
}
